cmake_minimum_required(VERSION 3.16)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    project(stlcompat VERSION 0.1.0 LANGUAGES CXX)
endif()

include(GNUInstallDirs)

if(NOT TARGET stlcompat)
    # Header-only interface library
    add_library(stlcompat INTERFACE)
    target_compile_features(stlcompat INTERFACE cxx_std_17)
endif()

# Provide namespaced alias for consumers (stlcompat::stlcompat)
if(TARGET stlcompat AND NOT TARGET stlcompat::stlcompat)
    add_library(stlcompat::stlcompat ALIAS stlcompat)
endif()

target_include_directories(stlcompat INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# Optional examples
option(STLCOMPAT_BUILD_EXAMPLES "Build stlcompat examples" OFF)
if(STLCOMPAT_BUILD_EXAMPLES)
    add_executable(stlcompat_test_print examples/test_print.cpp)
    target_link_libraries(stlcompat_test_print PRIVATE stlcompat)

    add_executable(stlcompat_test_span examples/test_span.cpp)
    target_link_libraries(stlcompat_test_span PRIVATE stlcompat)
endif()

# Install headers
install(DIRECTORY include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    FILES_MATCHING PATTERN "*.h"
)

# Export and install the interface target so downstream exports can reference it
install(TARGETS stlcompat
    EXPORT StlcompatTargets
)
install(EXPORT StlcompatTargets
    FILE stlcompatTargets.cmake
    NAMESPACE stlcompat::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/stlcompat
)
